package com.share.sharelibrary;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.share.sharelibrary.util.ShareSDKHelp;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

/**
 * 第三方登录
 *
 * @author QiQi
 */
public class ThirdLoginView extends LinearLayout implements AdapterView.OnItemClickListener {

    private GridView gridView;
    private Map<SHARE_MEDIA, Integer> platFormIcons;
    private ShareSDKHelp sdkHelp;
    private IGetPlatFormNameListener getPlatFormNameListener;
    private ShareSDKHelp.ILogin login;
    private View headView;

    public ThirdLoginView(Context context) {
        this(context, null);
    }

    public ThirdLoginView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ThirdLoginView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(context).inflate(R.layout.third_login_view, this, true);
        gridView = findViewById(R.id.grid);
        headView = findViewById(R.id.headView);
        gridView.setOnItemClickListener(this);
        if (isInEditMode()) {
            return;
        }
        sdkHelp = new ShareSDKHelp((FragmentActivity) context);
        sdkHelp.setLogin(new ShareSDKHelp.ILogin() {
            @Override
            public void onLogin(String access_token) {
                if (login != null) {
                    login.onLogin(access_token);
                }
            }
        });
    }
    public void setLogin(ShareSDKHelp.ILogin login) {
        this.login = login;
    }
    public void setColumnNum(int num) {
        gridView.setNumColumns(num);
    }


    public void onResult(int requestCode, int resultCode, Intent data) {
        sdkHelp.onResult(requestCode,resultCode,data);
    }



    public void hideHeadView() {
        headView.setVisibility(GONE);
    }

    public void setPlatForm(Map<SHARE_MEDIA, Integer> resId, SHARE_MEDIA... platForms) {
        if (resId == null || platForms == null) {
            throw new IllegalArgumentException("图片资源或平台列表不能为空");
        }

        LoginPlatFormAdapter adapter = new LoginPlatFormAdapter(getContext());
        platFormIcons = resId;
        if (platForms.length > 4) {
            gridView.setNumColumns(4);
        } else {
            gridView.setNumColumns(platForms.length);
        }

        adapter.addAll(platForms);
        gridView.setAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SHARE_MEDIA platform = (SHARE_MEDIA) parent.getAdapter().getItem(position);
        sdkHelp.doOauth(platform);
    }


    public void onDestroy() {
        sdkHelp.onDestroy();
        platFormIcons.clear();
        sdkHelp = null;
        this.getPlatFormNameListener = null;
    }

    private String getPlatName(SHARE_MEDIA platForm) {
        if (platForm.equals(SHARE_MEDIA.QQ)) {
            return ("QQ登录");
        } else if (platForm.equals(SHARE_MEDIA.WEIXIN)) {
            return ("微信登录");
        } else if (platForm.equals(SHARE_MEDIA.SINA)) {
            return ("新浪登录");
        } else if (getPlatFormNameListener != null) {
            return getPlatFormNameListener.getName(platForm);
        }

        return null;
    }

    /**
     * 实现此接口,以此来扩展获取平台名字
     */
    public interface IGetPlatFormNameListener {
        String getName(SHARE_MEDIA platForm);
    }

    private final class LoginPlatFormAdapter extends ArrayAdapter<SHARE_MEDIA> {

        public LoginPlatFormAdapter(Context context) {
            super(context, 0);
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.plat_form_item_layout, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            SHARE_MEDIA platForm = getItem(position);
            String platFormName = getPlatName(platForm);
            holder.platFormName.setText(platFormName);
            holder.platFormIcon.setImageResource(platFormIcons.get(platForm));
            return convertView;
        }


        class ViewHolder {
            public TextView platFormName;
            public ImageView platFormIcon;

            public ViewHolder(View v) {
                platFormIcon = (ImageView) v.findViewById(android.R.id.icon);
                platFormName = (TextView) v.findViewById(android.R.id.title);
            }
        }
    }
}
